﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace scmsByAspdNet.cotro.admin {
  /// <summary>
  /// getTeacherCourse 的摘要说明
  /// </summary>
  public class getTeacherCourse : IHttpHandler {

    public void ProcessRequest(HttpContext context) {
      context.Response.ContentType = "text/plain";
      //获取参数
      string tea_id = context.Request["tea_id"];
      //数据库
      SqlConnection conn = new SqlConnection("Data Source=106.54.69.143;database=scms;uid=sa;pwd=Aspdotnet666");
      conn.Open();

      SqlCommand sql_rows = new SqlCommand($"SELECT sc.id,sc.title,sc.credit,sc.hour,sc.attribute,sc.proportion,sc.faculty,sc.comment,sc.cover,sc.status FROM sc_teacher st,sc_cour_tea sct,sc_course sc WHERE st.id={tea_id} AND st.id=sct.tea_id AND sct.cour_id=sc.id", conn);
      string courses = "[";
      SqlDataReader reader = sql_rows.ExecuteReader();
      for (; reader.Read();) {
        courses += $"{{\"cour_id\":\"{reader[0].ToString()}\",\"title\":\"{reader[1].ToString()}\",\"credit\":\"{reader[2].ToString()}\",\"hour\":\"{reader[3].ToString()}\",\"attribute\":\"{reader[4].ToString()}\",\"proportion\":\"{reader[5].ToString()}\",\"faculty\":\"{reader[6].ToString()}\",\"comment\":\"{reader[6].ToString()}\",\"cover\":\"{reader[6].ToString()}\",\"status\":\"{reader[6].ToString()}\",\"is_deleted\":\"{reader[7].ToString()}\"}},";
      }
      if (courses.Length >= 4) {
        courses = courses.Substring(0, courses.Length - 1);
      }
      courses += "]";
      reader.Close();
      conn.Close();
      //返回数据
      string ret = "{" +
        "\"code\":\"1\"," +
        "\"courses\":" + courses + "" +
        "}";
      System.Console.WriteLine(ret);
      context.Response.StatusCode = 200;
      context.Response.Write(ret);
    }

    public bool IsReusable {
      get {
        return false;
      }
    }
  }
}